<template>
  <div class="examinationsWrap1">
    <div class="pageTableInner">
      <div class="tableContainer" v-if="checkItem!=2" style="top:0px;">
        <!--v-show="checkItem==5&&this.rows.length>0"-->
        <!--<div   style="float: right;margin-bottom: 10px;">
          <el-button v-if="$store.state.userInfo.roleLevel==1" type="primary" size="mini" @click="confirmExamine(1)">确认发布</el-button>
        </div>-->
        <div class="tableInner" ref="tableInner">
          <el-table
            ref="multipleTable"
            :data="rows"
            stripe
            show-summary
            :summary-method="getSummaries"
            :height="tableHeight"
            :span-method="objectSpanMethod"
            tooltip-effect="dark"
            style="width: 100%"
          >
            <el-table-column
              label="指标类别"
              prop="classifyName"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              label="类别分值"
              prop="classifyScore"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              prop="itemsName"
              label="指标项"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              prop="standardScore"
              label="指标分值"
              show-overflow-tooltip
            >
            </el-table-column>

            <el-table-column
              v-for="(item,key) in tableCompanyList"
              :key="item.id"
              :label="item.enterpriseName"
              :prop="'value'+key"
              show-overflow-tooltip>
              <template slot-scope="scope">
                <div v-if="scope.row.enterpriseTotalList" style="cursor: pointer;"  @click="$refs.detailDialog.dialogOpen({enterpriseNo:scope.row.enterpriseTotalList[key].enterpriseNo,itemsId:scope.row.itemsId,templateId:scope.row.enterpriseTotalList[key].templateId,taskId:scope.row.enterpriseTotalList[key].taskId,totalScore:scope.row.enterpriseTotalList[key].totalScore})">{{scope.row.enterpriseTotalList[key].totalScore}}</div>
              </template>
            </el-table-column>
          </el-table>

        </div>
      </div>

      <!--反馈意见-->
      <div v-show="checkItem==2" style=" padding: 20px;">
        <div v-show="taksList.length==0" style="margin-top: 100px;text-align: center;color: #3975FE;">
          <i class="iconfont icon-zanwushuju" style="font-size: 120px;"></i>
          <div>当前无反馈意见</div>
        </div>
        <div v-for="(item,index) in taksList" :key="index"  style="color: grey;line-height: 25px;border: 1px solid #ebeef5;margin-top: -1px;">
          <span style="width: 10%;display: inline-block;text-align: center;">{{item.agencyName}}</span>
          <span  style="width: 89%;border-left: 1px solid  #ebeef5;text-align: center;display: inline-block;">{{item.examineOpinion?item.examineOpinion:"无意见"}}</span>
          </div>
      </div>

    </div>

    <detailDialog ref="detailDialog"></detailDialog>
    <el-dialog :close-on-click-modal="false"
      title="不同意本次考核意见"
      :append-to-body="true"
      :visible.sync="dialogVisible"
      :before-close="handleClose">
      <el-input
        type="textarea"
        :rows="3"
        :maxlength="100"
        placeholder="请输入考核意见"
        v-model="examineOpinion">
      </el-input>

      <span slot="footer" class="dialog-footer">

    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="confirmExamineReult(0)">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
  import detailDialog from './detailDialog.vue';
  export default {
    props: ["examineTask","checkItem"],
    data(){
      return {
        rows: [],
//        currentStatus ,          //发布状态 0：未发布 1：预发布 3：部分已退回 5：已发布,
        multipleSelection: [],
        tableHeight: 400,
        spanArr: [],
        tableCompanyList: [],
        taksList: [],
        taskId:1,
        dialogVisible:false,
        examineOpinion:""
      }
    },
    components: {detailDialog},
    created(){

    },
    mounted(){
      this.$nextTick(()=>{
        //表格滚动
          this.tableHeight=document.querySelector(".taskManageWrapWrap").clientHeight-160
      })
    },

    methods: {
      handleClose(){
        this.examineOpinion=""
        this.dialogVisible = false
      },
      confirmExamine(hasAgree){
        if(hasAgree){ //同意
          this.$confirm('请确认是否正式发布考核结果？', '确认信息', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.confirmExamineReult(hasAgree)
          }).catch(() => {

          });
        }else {
          this.dialogVisible = true
        }

      },
      confirmExamineReult(hasAgree){
        this.axios.post(process.env.API_HOST + `/examine/releaseExamineResult`, {
          id: this.examineTask.id,
        })
          .then(response => {
            let result = response.data
            this.dialogVisible = false
            if (result.code == 200) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
            this.$emit("getTempalteInfo")
            }
          })
      },
      //获取所有反馈信息
      selectReleaseEnterpriseList(templateId, taskId){
        this.axios.post(process.env.API_HOST + `/examine/selectAgencyTask`, {
          templateId: templateId,
          taskId: taskId
        })
          .then(response => {
            let result = response.data
            if (result.code == 200) {
              this.taksList =result.result?result.result:[]
            }
          })

      },
      getExamineInfo(templateId, taskId){

        let url=""
        if(this.checkItem==1){  //预发布
          url="selectPreReleaseEnterpriseList"
        }else if(this.checkItem==5){ //正式
          url="selectReleaseEnterpriseList"
        }
        this.axios.post(process.env.API_HOST + `/examine/`+url, {
          templateId: templateId,
          taskId: taskId
        })
          .then(response => {
            let result = response.data
            if (result.code == 200) {
              if(!result.result||result.result.length==0){
                return
              }
              result.result.forEach(item => {
                item.itemList.forEach(item1 => {
                  this.rows.push({
                    ...item1,
                    classifyId: item.classifyId,
                    classifyName: item.classifyName,
                    classifyScore: ""
                  })
                })

              })
              this.tableCompanyList=[]
              if (this.rows.length > 0) {
                this.rows[0].enterpriseTotalList.forEach((item,key)=>{
                  this.$store.state.companyInfos.forEach(item1=>{
                    if(item.enterpriseNo==item1.enterpriseNo){
                      this.tableCompanyList.push({enterpriseName:item1.enterpriseName,totalScore:item.totalScore})
                    }
                  })
                })
                this.spanArr = this.getSpanArr(this.rows, "classifyName")
                this.rows.forEach((item, key) => {
                  this.tableCompanyList.forEach((item1,index)=>{
                    item['value'+index]=item.enterpriseTotalList[index].totalScore
                  })
                  this.totalScore += item.standardScore
                  this.getclassifyScore(key)
                })

                this.totalScore=this.toDecimal(this.totalScore)
              }
            }
          })

      },
      objectSpanMethod({row, column, rowIndex, columnIndex}) {
        if (columnIndex === 0 || columnIndex === 1) {
          const _row = this.spanArr[rowIndex];
          const _col = _row > 0 ? 1 : 0;
          return {
            rowspan: _row,
            colspan: _col
          };
        }
      },
      getclassifyScore(index){
        let sub = 0, isPass = true, classifyScore = 0
        this.spanArr.forEach(item => {
          sub += item
          if (isPass && sub > index) {
            isPass = false
            const sliceArr = this.rows.slice(sub - item, sub)
            sliceArr.forEach(item => {
              classifyScore += this.toDecimal(item.standardScore)
            })
            sliceArr.forEach(item => {
              item.classifyScore = classifyScore
            })
          }
        })

      },
      getSummaries(param) {
        let _this=this
        const {columns, data} = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '';
            return;
          } else if (index === 1) {
            sums[index] = '考核成绩';
            return;
          }
          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return _this.toDecimal(prev + curr) ;
              } else {
                return prev;
              }
            }, 0);
            sums[index] += " 分"
          } else {
//            sums[index] = 'N/A';
            sums[index] = '';
          }
        });

        return sums;
      },

    },
    watch:{
      checkItem(){
        if(this.checkItem==1){  //预发布
          this.rows = []
          setTimeout(()=>{
            this.getExamineInfo(this.examineTask.templateId,this.examineTask.id)
          },100)
          this.tableHeight=document.querySelector(".taskManageWrapWrap").clientHeight-220
        }else if(this.checkItem==5){ //正式发布
          this.rows = []
          setTimeout(()=>{
            this.getExamineInfo(this.examineTask.templateId,this.examineTask.id)
          },100)
          this.tableHeight=document.querySelector(".taskManageWrapWrap").clientHeight-220
        }else {
          setTimeout(()=>{
            this.selectReleaseEnterpriseList(this.examineTask.templateId,this.examineTask.id)
          },100)
        }
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "../../../common/stylus/mixin.styl"
  .examinationsWrap1
    page1()


</style>
